import 'dart:io';

import 'package:dio_cache_interceptor_file_store/dio_cache_interceptor_file_store.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_cache/flutter_map_cache.dart';
import 'package:path_provider/path_provider.dart';

class ImagingLayer  {


  static getImagingLayer(){
    return
      [
        FutureBuilder<Directory>(
          future: getTemporaryDirectory(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final dataPath = snapshot.requireData.path;
              return TileLayer(
                urlTemplate:
                "http://t4.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=5cdb30ab6547d28d8058daf1279a1ab4",
                tileProvider: CachedTileProvider(
                  maxStale: const Duration(days: 99999),
                  store: FileCacheStore(
                    '${dataPath}${Platform.pathSeparator}IMGWFileCacheStore',
                  ),
                ),
              );
            } else {
              return TileLayer(
                urlTemplate:
                "http://t4.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=5cdb30ab6547d28d8058daf1279a1ab4",
              );
            }
          },
        ),

        FutureBuilder<Directory>(
          future: getTemporaryDirectory(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final dataPath = snapshot.requireData.path;
              return TileLayer(
                urlTemplate:
                "http://t4.tianditu.gov.cn/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=5cdb30ab6547d28d8058daf1279a1ab4",
                tileProvider: CachedTileProvider(
                  maxStale: const Duration(days: 99999),
                  store: FileCacheStore(
                    '${dataPath}${Platform.pathSeparator}CIAWFileCacheStore',
                  ),
                ),
              );
            } else {
              return TileLayer(
                urlTemplate:
                "http://t4.tianditu.gov.cn/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=5cdb30ab6547d28d8058daf1279a1ab4",
              );
            }
          },
        ),
    ];
  }


  // 地形图：http://t4.tianditu.gov.cn/DataServer?T=ter_w&x={x}&y={y}&l={z}&tk=key
  // 街道图：http://t4.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=key
  // 影像图：http://t4.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=key
  // 影像标注：http://t4.tianditu.gov.cn/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=key
  // 地形标注：http://t4.tianditu.gov.cn/DataServer?T=cta_w&x={x}&y={y}&l={z}&tk=key
  // 矢量标注：http://t4.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=key


  static getDztLayer(){
    return
      [
        FutureBuilder<Directory>(
          future: getTemporaryDirectory(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final dataPath = snapshot.requireData.path;
              return TileLayer(
                urlTemplate:
                "http://t4.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=5cdb30ab6547d28d8058daf1279a1ab4",
                tileProvider: CachedTileProvider(
                  maxStale: const Duration(days: 99999),
                  store: FileCacheStore(
                    '${dataPath}${Platform.pathSeparator}VECWFileCacheStore',
                  ),
                ),
              );
            } else {
              return TileLayer(
                urlTemplate:
                "http://t4.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=5cdb30ab6547d28d8058daf1279a1ab4",
              );
            }
          },
        ),

        FutureBuilder<Directory>(
          future: getTemporaryDirectory(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final dataPath = snapshot.requireData.path;
              return TileLayer(
                urlTemplate:
                "http://t4.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=5cdb30ab6547d28d8058daf1279a1ab4",
                tileProvider: CachedTileProvider(
                  maxStale: const Duration(days: 99999),
                  store: FileCacheStore(
                    '${dataPath}${Platform.pathSeparator}CVAWFileCacheStore',
                  ),
                ),
              );
            } else {
              return TileLayer(
                urlTemplate:
                "http://t4.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=5cdb30ab6547d28d8058daf1279a1ab4",
              );
            }
          },
        ),
      ];
  }

}

